<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "billing");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style >
	#outStockInfoCopy{display:none}
	.thumbnail{margin: 0px}
	#logo{margin: 0px;padding: 0px;}
	#pic{padding: 0px;height:60px;width:60px;}
	td input{margin-top:-1px;margin-bottom:-1px;height:21px; width:98%;border:none;color:red}
	#area{
		height:362px;
		width:1180px;
		overflow-y:auto; 
		overflow-x:hidden; 
	}
</style>
	
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
	<div class="detailed-navbar-content panel-body">
		<input type="hidden" value='${stocksJson}' id="stocksJson" />
		<input type="hidden" value='${typeJson}' id="typeJson" />
		<input type="hidden" value='${billingJson}' id="billingJson" />
		<t:form modelAttribute="billing" action="/billing/billing/save" method="post" cssClass="form-horizontal">
			<div class="hide">
				 <t:hidden path="billingId"/>
 				 <t:hidden path="deleteMark"/>	
 				 <t:hidden path="createUserName"/>
		 		 <t:hidden path="createUserId"/>
		 		 <t:hidden path="createDate"/>
		 		 <t:hidden path="shopId"/>
			</div>
            <t:formgroup>
                <t:label path="customerId" cssClass="col-sm-1 col-xs-1" />
                <t:select2 path="customerId" placeholder="请填写客户" allowClear="true" colClass="col-sm-2 col-xs-2"></t:select2>
                <t:label path="deliveryDate" cssClass="col-sm-1 col-xs-1" />
                <t:date  path="deliveryDate" colClass="col-sm-2 col-xs-2"></t:date>
                <t:label path="businessId" cssClass="col-sm-1 col-xs-1" />
                <t:select2 path="businessId" placeholder="业务员" allowClear="true" colClass="col-sm-2 col-xs-2"></t:select2>
                <t:label path="billingMoney" cssClass="col-sm-1 col-xs-1" />
				<t:input path="billingMoney" colClass="col-sm-2 col-xs-2" />
            </t:formgroup>
            <t:formgroup>
                <t:label path="discount" cssClass="col-sm-1 col-xs-1" />
				<t:input path="discount" colClass="col-sm-2 col-xs-2" />
				<t:label path="comment" cssClass="col-sm-1 col-xs-1" />
				<t:input path="comment" colClass="col-sm-2 col-xs-2" />
            </t:formgroup>
            <div id="area">
            <table  class="cntable table table-bordered dataTable table-billing">
	            <thead>
	            	<th style="width:50px"></th>
	            	<th style="width:50px">操作</th>
	            	<th style="width:55px">主图</th>
	            	<th style="width:250px">商品编码</th>
	            	<th style="width:50px">商品名称</th>
	            	
	            	<th style="width:150px">规格</th>
	            	<th style="width:50px">数量</th>
	            	<th style="width:50px">单价</th>
	            	<th style="width:50px">小计</th>
	            	<th style="width:50px">备注</th>
	            	
	            </thead>
	            
	            <tbody id="outStockInfo">
	            	<c:if test="${type eq 'add' }" >
						<tr id="outStockCollocation_0">
							<td style="display:none"><input id="collocationId_0" name="collocationId" type="text" value="${outStockCollocation.collocationId}"/></td>
							<td style="display:none"><input id="cargoId_0" name="cargoId" type="text" value="${cargoId}"/></td>
							<td>1</td>
							<td id="logo" >
				            	<div class="col-md-6 col-sm-6" onclick="addData()" ><i class="fa fa-plus-square"></i></div>
				            	<div class="col-md-6 col-sm-6" onclick="deleteData('outStockCollocation_0')"><i class="fa fa-minus-square"></i></div>
			            	</td>
			            	<td id="pic"  data-trigger="hover"  >
		               			<div class="thumbnail">								
									<img src="<c:url value='/images/hu.jpg'/>" alt="">
								</div>
		               		</td>							
			            	<td><select style="ime-mode:disabled" id="cargoNo_0" name="cargoNo" type="text" value="${outStockCollocation.collocationId}"><option value=""></option></select></td>
			            	<td><input style="ime-mode:disabled" disabled="disabled" id="cargoName_0" name="cargoName" type="text" value="${outStockShow.cargo.cargoName}"/></td>
			            	<td><input style="ime-mode:disabled" disabled="disabled" id="propertyName_0" name="propertyName" type="text" value="${outStockCollocation.collocation.propertyNameOne}/${outStockCollocation.collocation.propertyNameTwo}"/></td>
							
							<td><input style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="collocationNum_0"  name="collocationNum" type="text" value="${outStockCollocation.collocationNum}"/></td>
							<td><input style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="realPrice_0" name="realPrice" type="text" value="${outStockCollocation.realPrice}"/></td>
							<td><input style="ime-mode:disabled" id="subtotal_0" name="subtotal" type="text" value=""/></td>
							<td><input style="ime-mode:disabled" id="comment_0" name="comments" type="text" value="${outStockCollocation.comment}"/></td>
						</tr>
					</c:if>
					<c:set var="sequence"  value="0"/>
					<c:if test="${type eq 'modify' || type eq 'check'}" >
						<c:forEach var="outStockShow" items="${billing.outStock.outStockShows}">
							<c:forEach var="outStockCollocation" items="${outStockShow.outStockCollocations}" >
								
								<tr id="outStockCollocation_${sequence}">
									<td style="display:none"><input id="collocationId_${sequence}" name="collocationId" type="text" value="${outStockCollocation.collocationId}"/></td>
									<td style="display:none"><input id="cargoId_${sequence}" name="cargoId" type="text" value="${outStockShow.cargoId}"/></td>
									<td>${sequence+1}</td>
									<td id="logo" >
						            	<div class="col-md-6 col-sm-6" onclick="addData()" ><i class="fa fa-plus-square"></i></div>
						            	<div class="col-md-6 col-sm-6" onclick="deleteData('outStockCollocation_${sequence}')"><i class="fa fa-minus-square"></i></div>
					            	</td>
					            	<td id="pic"  data-trigger="hover"  >
				               			<div class="thumbnail">								
											<img src="<c:url value='/images/hu.jpg'/>" alt="">
										</div>
				               		</td>							
					            	<td><input style="ime-mode:disabled" id="cargoNo_${sequence}" name="cargoNo" type="text" value="${outStockShow.cargo.cargoNo}"></input></td>
					            	<td><input style="ime-mode:disabled" disabled="disabled" id="cargoName_${sequence}" name="cargoName" type="text" value="${outStockShow.cargo.cargoName}"/></td>
					            	<td><input style="ime-mode:disabled" disabled="disabled" id="propertyName_${sequence}" name="propertyName" type="text" value="${outStockCollocation.collocation.propertyNameOne}/${outStockCollocation.collocation.propertyNameTwo}"/></td>
									
									<td><input style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="collocationNum_${sequence}"  name="collocationNum" type="text" value="${outStockCollocation.collocationNum}"/></td>
									<td><input style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="realPrice_${sequence}" name="realPrice" type="text" value="${outStockCollocation.realPrice}"/></td>
									<td><input style="ime-mode:disabled" id="subtotal_${sequence}" name="subtotal" type="text" value="${outStockCollocation.realPrice*outStockCollocation.collocationNum}"/></td>
									<td><input style="ime-mode:disabled" id="comment_${sequence}" name="comments" type="text" value="${outStockCollocation.comment}"/></td>
								</tr>
								<c:set var="sequence"  value="${sequence +1}"/>
							</c:forEach>
						</c:forEach>
						
					</c:if>
				</tbody>
            </table>
			</div>
		</t:form>
	</div>

</t:panel>
<table id="outStockInfoCopy">
	<tbody id="outStockInfoCopyBody">
		<tr id="outStockCollocation_temp">
			<td style="display:none"><input id="collocationId_temp" name="collocationId" type="text" value="${outStockCollocation.collocationId}"/></td>
			<td style="display:none"><input id="cargoId_temp" name="cargoId" type="text" value="${cargoId}"/></td>
			<td>1</td>
			<td id="logo" >
		          	<div class="col-md-6 col-sm-6" onclick="addData()" ><i class="fa fa-plus-square"></i></div>
		          	<div class="col-md-6 col-sm-6" onclick="deleteData('outStockCollocation_temp')"><i class="fa fa-minus-square"></i></div>
		         	</td>
		         	<td id="pic"  data-trigger="hover"  >
		           			<div class="thumbnail">								
					<img src="<c:url value='/images/hu.jpg'/>" alt="">
				</div>
           	</td>							
         	<td><select style="ime-mode:disabled" id="cargoNo_temp" name="cargoNo" type="text" value="${outStockCollocation.collocationId}"><option value=""></option></select></td>
         	<td><input style="ime-mode:disabled" disabled="disabled" id="cargoName_temp" name="cargoName" type="text" value="${outStockShow.cargo.cargoName}"/></td>
         	<td><input style="ime-mode:disabled" disabled="disabled" id="propertyName_temp" name="propertyName" type="text" value="${outStockCollocation.collocation.propertyNameOne}/${outStockCollocation.collocation.propertyNameTwo}"/></td>
	
			<td><input style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="collocationNum_temp"  name="collocationNum" type="text" value="${outStockCollocation.collocationNum}"/></td>
			<td><input style="ime-mode:disabled"  onkeyup="return ValidateNumber(this,value)" id="realPrice_temp" name="realPrice" type="text" value="${outStockCollocation.realPrice}"/></td>
			<td><input style="ime-mode:disabled" id="subtotal_temp" name="subtotal" type="text" value=""/></td>
			<td><input style="ime-mode:disabled" id="comment_temp" name="comments" type="text" value="${outStockCollocation.comment}"/></td>
		</tr>
	</tbody>
</table>
<script type="text/javascript">

$(function(){
	outStockInfo = $("#outStockInfoCopyBody").html();
	$('#pic').popover({
	     trigger : 'hover',//鼠标以上时触发弹出提示框
	     html:true,//开启html 为true的话，data-content里就能放html代码了
	     content:"<img src='<c:url value='/images/hu.jpg'/>' >"
	 });
	
	var productNo=$('#productNo').text();
	if(!$.isNull(productNo)){
	 showMark=true;
	 
	}
	
	$("[name='realPrice']").change(function(){
		/* alert($("[name='realPrice']").val()); */
	});
	$("#discount").change(function(){
		updateBillingMoney();
	});
	
	if(!$.isNull($("#typeJson").val())){
		var type =JSON.parse($("#typeJson").val());
		if(type=="modify"){
			loadBilling();
		}
		if(type=="add"){
			stocksShow(0);
		}
	}
})

function loadBilling(){
	num--;
	if(!$.isNull($("#billingJson").val())){
		var billing =JSON.parse($("#billingJson").val());

		billing.outStock.outStockShows.forEach(function(element,index){
			element.outStockCollocations.forEach(function(outStockCollocation,index){
				num++;
				updateSubtotal(num);

			});
		});
	}
		
}

var outStockInfo;
var newOutStockInfo;
var num=0;
function addData(){
	num++;
	
	newOutStockInfo=outStockInfo.replace(/1/g,num+1);
	newOutStockInfo=newOutStockInfo.replace(/temp/g,num);

	$("#outStockInfo").append(newOutStockInfo);
	stocksShow(num);
}

function deleteData(deleteId){
	$("#"+deleteId).remove();
	var deleteNum = parseInt(deleteId.substring(20));
	updateBillingMoney();
 	/* for(var i=1;i<=num;i++){
		if(i>deleteNum){
			console.log(i);
			/* var outStockInfoDeleteNum = $("#outStockCollocation_"+i).html();
			outStockInfoDeleteNum=outStockInfoDeleteNum.replace(new RegExp(i,"gm"),i-1);
			$("#outStockCollocation_"+i).html(outStockInfoDeleteNum);
			$("#outStockCollocation_"+i).removeClass("outStockCollocation_"+i).addClass("outStockCollocation_"+(i-1)); 
		}
	} */ 
}


function saveData(lBtn){
	var isRepetition = false;
	var collocationIds=[];
	$("input[name='collocationId']").each(function(){
		collocationIds.push({
			id:$(this).val(),
			text:$(this).val()
		})
	});

	for(var i=0;i<collocationIds.length;i++){
		console.log(collocationIds[i].id);
		for(var j=i+1;j<collocationIds.length;j++){
			if(collocationIds[i].id==collocationIds[j].id){
				isRepetition=true;
			}
		}
	}
	if(isRepetition){
		$.showTips("存在相同货品！",1000,"warning");
		return;
	}
	
	var isSave=false;
	$("input[name='collocationId']").each(function(){
		if($(this).val()!=null && $(this).val() != ""){
			isSave=true;
		}
						
	});
	if(!isSave){
		$.showTips("请选择开单货品！",1000,"warning");
		return;
	}

	if (!$("form:first").valid()){
		return;
	}
	lBtn.start();
	$("form:first").submit();
}	

var showMark=true;

var stuffData=[];
function stocksShow(showNum){
	stuffData=[];
	if(!$.isNull($("#stocksJson").val())){
		loadStock();
	}
	var stocksJson =JSON.parse($("#stocksJson").val());
	var select=$('#cargoNo_'+showNum).select2({placeholder:"请选择货物信息",allowClear: true ,data : stuffData});
	select.change(function(){
		var collocationId = $('#cargoNo_'+showNum).val();
		var repetitionNum = 0;
		$("select[name='cargoNo']").each(function(){

			if($(this).val()==collocationId){
				repetitionNum++;
			}
		});
		stocksJson.forEach(function(element,index){
			element.stockCollocations.forEach(function(stockCollocation,index){
				if(stockCollocation.collocationId==collocationId){
					
					$('#cargoId_'+showNum).val(element.cargoId);
					$('#collocationId_'+showNum).val(stockCollocation.collocationId);
					/* $('#cargoNo_'+showNum).val(element.cargo.cargoNo); */
					$('#cargoName_'+showNum).val(element.cargo.cargoName);
					$('#propertyName_'+showNum).val(stockCollocation.collocation.propertyNameOne + "/" + stockCollocation.collocation.propertyNameTwo);
					$('#collocationNum_'+showNum).val(1);
					$('#realPrice_'+showNum).val(stockCollocation.collocation.salePrice);
					$('#subtotal_'+showNum).val(stockCollocation.collocation.salePrice*1);
				}
			});
		});
		updateBillingMoney();
		if(repetitionNum>1){
			$.showTips("此货品已存在！",1000,"warning");
		}

	});
	updateSubtotal(showNum);
	

}

function loadStock(){
	var stocksJson =JSON.parse($("#stocksJson").val());

	stocksJson.forEach(function(element,index){
		element.stockCollocations.forEach(function(stockCollocation,index){
			stuffData.push({
				id:stockCollocation.collocationId,
				text:element.cargo.cargoNo +"  "+ element.cargo.cargoName +"  " + stockCollocation.collocation.propertyNameOne + "/" + stockCollocation.collocation.propertyNameTwo +"  "+ stockCollocation.collocation.salePrice
			})
		});
	});
}

function updateSubtotal(showNum){
	$('#collocationNum_'+showNum).bind('input propertychange', function() {
		var count = parseInt($(this).val());
		$(this).val(count);
		if(isNaN(count)){
			count = 0;
			$(this).val(0);
		}
		var price = $('#realPrice_'+showNum).val();
		$('#subtotal_'+showNum).val(price*count);
		updateBillingMoney();
	});
	$('#realPrice_'+showNum).bind('input propertychange', function() {
		var price = parseFloat($(this).val());

		if(isNaN(price)){
			price = 0;
			$(this).val(0);
		}
		var count = $('#collocationNum_'+showNum).val();
		$('#subtotal_'+showNum).val(price*count);
		updateBillingMoney();
	});
	$('#collocationNum_'+showNum).focus();
}

function updateBillingMoney(){
	var billingMoney = 0;
	var discount = $("#discount").val();
	if(discount == "" || discount == null || discount == undefined || discount==0){
		$("input[name='subtotal']").each(function(){
			if($(this).val()!=null && $(this).val() != ""){
				billingMoney+= parseFloat($(this).val());
			}
							
		});

		$("#billingMoney").val(billingMoney);
    }else{
    	
    	$("input[name='subtotal']").each(function(){
    		if($(this).val()!=null && $(this).val() != ""){
				billingMoney+= parseFloat($(this).val());
			}			
		});
    	billingMoney=billingMoney*(discount/10);
		$("#billingMoney").val(billingMoney);
    }
}

function ValidateNumber(e, pnumber){
	if (!/^\d+[.]?\d*$/.test(pnumber))
	{
	e.value = /^\d+[.]?\d*/.exec(e.value);
	}else{
		
 	}
	return false;
}
</script>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formtags.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formpickers.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>